var gulp = require('gulp');
var responsive = require('gulp-responsive');
var del = require('del');

gulp.task('clean:output', function () {
    return del([
        'public/res-img/**/*',
        'public/style-img/**/*',
    ]);
});

/*
这个插件貌似没有针对png的压缩支持
*/

const commonLevelCfg = [
    {
        width: 32,
        quality: 10,
        blur: true,
        rename: {
            suffix: '-32'
        },
    },
    {
        width: 180,
        quality: 80,
        compressionLevel: 6,
        rename: {
            suffix: '-180'
        }
    },
    {
        width: 360,
        quality: 80,
        compressionLevel: 6,
        rename: {
            suffix: '-360'
        },
    },
    {
        width: 768,
        quality: 80,
        compressionLevel: 9,
        rename: {
            suffix: '-768'
        },
    },
    {
        width: 1250,
        quality: 60,
        compressionLevel: 9,
        rename: {
            suffix: '-1250'
        },
    },
    {
        compressionLevel: 9,
        rename: {
            suffix: '-original'
        }
    }
];

gulp.task('res-img', function () {
    return gulp
        .src('./public/res-img-src/*.{png,jpg}')
        .pipe(
            responsive({
                '*': commonLevelCfg
            }, {
                errorOnEnlargement: false,
                skipOnEnlargement: true,
                withoutEnlargement: true
            })
        )
        .pipe(gulp.dest('./public/res-img/'))
});

gulp.task('style-img', function () {
    return gulp
        .src('./public/style-img-src/*.{png,jpg}')
        .pipe(
            responsive({
                '*': commonLevelCfg
            }, {
                errorOnEnlargement: false,
                skipOnEnlargement: true,
                withoutEnlargement: true
            })
        )
        .pipe(gulp.dest('./public/style-img/'))
});


//编排默认任务,串行这些任务
gulp.task("default", gulp.series('clean:output', 'res-img', 'style-img'));